package com.qianfeng.gl4study.youshare.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * 通用的显示Adapter
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/4
 * Email:pdsfgl@live.com
 */
public class CommonImagePagerAdapter extends PagerAdapter {

    private List<Integer> imageIds;
    private Context context;

    public CommonImagePagerAdapter(List<Integer> imageIds, Context context) {
        this.imageIds = imageIds;
        this.context = context;
    }

    @Override
    public int getCount() {
        int count = 0;
        if(imageIds!=null){
            count = imageIds.size();
        }
        return count;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return object == view;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView ret = new ImageView(context);

        ret.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ret.setImageResource(imageIds.get(position));

        container.addView(ret);

        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if(object instanceof ImageView){
            container.removeView((ImageView)object);
        }
    }
}
